/*************************************************************
 * 
 * CopyRight(c)2007, http://www.moorwind.com/
 * email:ezhung@gmail.com
 * for free, but a email permission would be nice
 * 
 ************************************************************/
package kono.utils
{
	public class DoubleNode
	{
		//双向链表， double list node
				
		private var _data:*;
		private var _prev:DoubleNode;
		private var _next:DoubleNode;
		
		//@initData:结点初始值， the node' data;
		public function DoubleNode(initData:*)
		{
			_data = initData;
			_prev = _next = null;
		}
		
		public function get next():DoubleNode
		{
			return _next;
		}
		
		public function get prev():DoubleNode
		{
			return _prev;
		}
		
		public function set next(newNode:DoubleNode):void
		{
			_next = newNode;
		}
		
		public function set prev(newNode:DoubleNode):void
		{
			_prev = newNode;
		}
		
		//取得当前结点数据, return the node's data
		public function get nodeData():*
		{
			return _data;
		}
		
		//设置当前结点数据， set the node‘s data
		public function set nodeData(newData:*):void
		{
			_data = newData;
		}
		
		//在当前节点后插入结点， append a new node after current node
		public function addAfter(newNode:DoubleNode):void
		{
			newNode.next = _next;
			newNode.prev = this;
			if(_next) _next.prev = newNode;
			_next = newNode;
			
		}
		
		//在当前节点前插入结点， append a new node before current node
		public function addBefore(newNode:DoubleNode):void
		{
			newNode.next = this;
			newNode.prev = _prev;
			if(_prev) _prev.next = newNode;
			_prev = newNode;
		}
		
		//返回当前数组长度， get the length of the node
		public  function get nodeLength():uint
		{
			var cursor:DoubleNode;
			var length:uint = 1;
			
			for(cursor = _next; cursor != null; cursor = cursor.next)
			{
				length++;
			}
			
			return length;
		}
		
		//从双向链表中脱离。 get out of link to the double-node
		public function unlink():void
		{
			if(_prev) _prev.next = _next;
			if(_next) _next.prev = _prev;
			_next = _prev = null;
		}
		
		//描述当前double-node， rerurn a string represent the double-node		
		public function toString():String
		{
			return "[DoubleNode, data=" + _data + "]"
		}
	}
}